﻿Partial Public Class frmStock_Popup
    Inherits System.Web.UI.Page
    Dim conn As New System.Data.SqlClient.SqlConnection(connString)

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session("MAT_USE") Is Nothing Then
                ViewState("UseMAT") = CreateDataset()
            Else
                Try
                    If (Session("MAT_USE") = "") Then
                        ViewState("UseMAT") = CreateDataset()
                    Else
                        If (CType(Session("MAT_USE"), DataSet).Tables(0).Rows.Count = 0) Then
                            ViewState("UseMAT") = CreateDataset()
                        Else
                            ViewState("UseMAT") = Session("MAT_USE")
                        End If
                    End If
                Catch ex As Exception
                    If (CType(Session("MAT_USE"), DataSet).Tables(0).Rows.Count = 0) Then
                        ViewState("UseMAT") = CreateDataset()
                    Else
                        ViewState("UseMAT") = Session("MAT_USE")
                    End If
                End Try


            End If

            LoadData()
            gridMatuse.DataSource = ViewState("UseMAT")
            gridMatuse.DataBind()
        End If
    End Sub
    Public Sub LoadData()

        Dim sql As String = "Select * From Op_Product"
        Dim da As New System.Data.SqlClient.SqlDataAdapter(sql, conn)
        Dim dt As New DataSet
        Dim ds As New DataSet
        da.Fill(dt)
        If Not Session("MAT_USE") Is Nothing Then
            ds = Session("MAT_USE")

            For a As Integer = 0 To ds.Tables(0).Rows.Count - 1
                If dt.Tables(0).Rows(a).Item("Op_ProductID") = ds.Tables(0).Rows(a).Item("Op_ProductID") Then
                    dt.Tables(0).Rows(a).Delete()
                End If
            Next
        End If

        ViewState("pProduct") = dt

        With gridTariff
            .DataSource = ViewState("pProduct")
            '.DataMember = "product"
            .DataBind()

        End With
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('1');", True)
    End Sub
    Public Function CreateDataset() As DataSet
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim dr As DataRow
        ds.Tables.Add(dt)
        dt.Columns.Add("Op_ProductID", GetType(String))
        dt.Columns.Add("Op_ProductName", GetType(String))
        dt.Columns.Add("Count", GetType(String))
        dt.Columns.Add("Op_ProductUnit", GetType(String))
        dt.Columns.Add("Op_ProductAmount", GetType(String))
        dt.Columns.Add("Op_Concentration", GetType(String))


        dr = ds.Tables(0).NewRow
        dr("Op_ProductID") = ""
        dr("Op_ProductName") = ""
        dr("Count") = ""
        dr("Op_ProductUnit") = ""
        dr("OP_ProductAmount") = "0"
        dr("OP_Concentration") = "0"


        ds.Tables(0).Rows.Add(dr)
        ds.Tables(0).AcceptChanges()
        Return ds
    End Function
    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridTariff.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridTariff.PageIndex = 0
            Case "prev"
                If (gridTariff.PageIndex <> 0) Then
                    gridTariff.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridTariff.PageIndex <> gridTariff.PageCount) Then
                    gridTariff.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridTariff.PageIndex = gridTariff.PageCount
        End Select
        gridTariff.DataSource = ViewState("pProduct")
        gridTariff.DataBind()
    End Sub
    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridTariff.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridTariff.PageIndex = ddlPage.SelectedIndex
        gridTariff.DataSource = ViewState("pProduct")
        gridTariff.DataBind()
    End Sub

    Private Sub gridTariff_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTariff.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim IndexDelete As Integer = Index
        If (gridTariff.PageIndex <> 0) Then
            Index = Index Mod (gridTariff.PageIndex * 10)
        End If
        If (e.CommandName = "cmdadd") Then
            ViewState("NotMAT") &= ",'" & gridTariff.Rows(Index).Cells(0).Text & "'"
            LoadDataCountMAtUSe()
            Dim Useds As DataSet = ViewState("UseMAT")
            If (Useds.Tables(0).Rows(0).Item("Op_ProductID") & "" = "") Then
                'กรณี แถวแรก
                Useds.Tables(0).Rows(0).Item("Op_ProductID") = gridTariff.Rows(Index).Cells(0).Text
                Useds.Tables(0).Rows(0).Item("Op_ProductName") = gridTariff.Rows(Index).Cells(1).Text
                Useds.Tables(0).Rows(0).Item("Op_ProductUnit") = gridTariff.Rows(Index).Cells(2).Text
                Useds.Tables(0).Rows(0).Item("Op_ProductAmount") = gridTariff.Rows(Index).Cells(4).Text
                Useds.Tables(0).Rows(0).Item("Count") = "1"

            Else
                'กรณีแถวสอง
                Dim dr As DataRow = Useds.Tables(0).NewRow()
                dr.Item("Op_ProductID") = gridTariff.Rows(Index).Cells(0).Text
                dr.Item("Op_ProductName") = gridTariff.Rows(Index).Cells(1).Text
                dr.Item("Op_ProductUnit") = gridTariff.Rows(Index).Cells(2).Text
                dr.Item("Op_ProductAmount") = gridTariff.Rows(Index).Cells(4).Text
                dr.Item("Count") = "1"
                Useds.Tables(0).Rows.Add(dr)
                Useds.AcceptChanges()
            End If
            ViewState("UseMAT") = Useds
            Dim dsDelete As DataSet = ViewState("pProduct")
            dsDelete.Tables(0).Rows(IndexDelete).Delete()
            dsDelete.AcceptChanges()
            ViewState("Tariff") = dsDelete
            gridTariff.DataSource = ViewState("pProduct")
            gridTariff.DataBind()
            gridMatuse.DataSource = ViewState("UseMAT")
            gridMatuse.DataBind()
        End If
    End Sub

    Private Sub LoadDataCountMAtUSe()
        Dim ds As DataSet = ViewState("UseMAT")
        For i = 0 To gridMatuse.Rows.Count - 1
            Dim txt As TextBox = CType(gridMatuse.Rows(i).FindControl("txtCount"), TextBox)
            ds.Tables(0).Rows(i).Item("Count") = txt.Text
            ds.AcceptChanges()
        Next
        ViewState("UseMAT") = ds
    End Sub

    Private Sub gridTariff_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTariff.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridTariff.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridTariff.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridTariff.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridTariff.PageCount.ToString()
            End If
        End If
    End Sub

    Protected Sub btnSearchTariff_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearchTariff.Click
        LoadData()
    End Sub

    Private Sub gridMatuse_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMatuse.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim txt As TextBox = CType(e.Row.FindControl("txtCount"), TextBox)
            If (gridMatuse.DataKeys(e.Row.RowIndex).Values("Op_ProductID") & "" = "") Then
                txt.Visible = False
                e.Row.Cells(0).Text = ""
            End If
        End If
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Dim dsTariff As DataSet = Session("MAT_USE")
        LoadDataCountMAtUSe()
        If Not (dsTariff Is Nothing) Then
            dsTariff.Tables(0).Merge(CType(ViewState("UseMAT"), DataSet).Tables(0))
            dsTariff.AcceptChanges()
            Session("MAT_USE") = dsTariff
        Else
            Session("MAT_USE") = ViewState("UseMAT")
        End If
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('3');", True)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class